package com.myapp.web.action.nag;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.myapp.model.entity.product.Product;
import com.myapp.service.product.ProductBo;
import com.myapp.util.ShoppingCartRegister;
import com.opensymphony.xwork2.ActionSupport;

public class CartAction extends ActionSupport implements ServletRequestAware{
	
	private static final long serialVersionUID = 4675291011850562729L;
	private Map<Integer,Integer> shoppingCart = null;
	private List<Product> products = null;
	private ProductBo prodBo;
	private String p;
	private HttpServletRequest request = null;
		
	public String addItem() throws Exception {
		int id = Integer.valueOf(p);
		if(id != 0){
			addItem(id);
		}
		return listItem();
	}
	
	public List<Product> getProducts() {
		return products;
	}

	public void setProdBo(ProductBo prodBo) {
		this.prodBo = prodBo;
	}

	public String listItem() throws Exception { 
		String sessionId = request.getSession().getId();
		shoppingCart = ShoppingCartRegister.instance().getCart(sessionId);		
		products = prodBo.getProduct(shoppingCart.keySet());
		return SUCCESS;
	}
	
	private void addItem(int k){
		String sessionId = this.request.getSession().getId();
		ShoppingCartRegister.instance().updateShoppingItem(sessionId, k, 1);
	}

	public void setP(String p) {
		this.p = p;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;	
	}
}
